<!-- 解决layer.open 不居中问题 -->
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>质量体系二类</title>
    <!--引入抽取css文件-->
    <%@include file="../common/public-css.jsp" %>

<body>

<div class="admin-main">

    <blockquote class="layui-elem-quote">
        <fieldset class="layui-elem-field">
            <legend>高级筛选</legend>
            <%--<div class="layui-field-box layui-form">--%>
                <%--<form class="layui-form" action="" id="formData">--%>
                    <%--<div class="layui-form-item">--%>
                        <%--<div class="layui-inline">--%>
                            <%--<label class="layui-form-label">选择公司</label>--%>
                            <%--<div class="layui-input-inline">--%>
                                <%--<select  id="companyId"  lay-search>--%>
                                    <%--<option value="">选择搜索公司</option>--%>
                                <%--</select>--%>
                            <%--</div>--%>
                        <%--</div>--%>
                        <%--<div class="layui-inline">--%>
                            <%--<label class="layui-form-label">部门名称</label>--%>
                            <%--<div class="layui-input-inline">--%>
                                <%--<input type="text"  id="departmentName" lay-verify=""--%>
                                       <%--placeholder="按部门名称搜索" autocomplete="off" class="layui-input"--%>
                                       <%--maxlength="50">--%>
                            <%--</div>--%>
                        <%--</div>--%>
                    <%--</div>--%>
                    <%--<div class="layui-form-item">--%>
                        <%--<div class="layui-input-block">--%>
                            <%--<button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i> 搜索</button>--%>
                            <%--<button type="reset" class="layui-btn layui-btn-primary">清空</button>--%>
                        <%--</div>--%>
                    <%--</div>--%>
                <%--</form>--%>
            <%--</div>--%>
        </fieldset>
    </blockquote>

    <fieldset class="layui-elem-field">
        <legend>质量体系二类&nbsp;<i class="fa fa-refresh" aria-hidden="true"></i></legend>
        </legend>
        <div class="layui-field-box layui-form">
            <blockquote class="layui-elem-quote">
                <button class="layui-btn layui-btn layui-btn-small layui-btn-normal hide checkBtn_13 addBtn"><i
                        class="layui-icon">
                    &#xe608;</i> 添加
                </button>
            </blockquote>
            <table id="dataTable" class="layui-table admin-table table-bordered display" cellspacing="0" width="100%">
                <thead>
                <tr>
                    <th>所属一类</th>
                    <th>二类名称</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </fieldset>
</div>

<!--引入抽取公共js-->
<%@include file="../common/public-js.jsp" %>
<script src="<%=request.getContextPath()%>/resources/js/common/jQuery.md5.js"></script>
<script>

    var form = null;
    var dataTable = null;
    var layer = null;
    var companyList = null;
    $(document).ready(function () {
        dataTable = $('#dataTable').DataTable({
            "searching": false, "bStateSave": true, //状态保存，使用了翻页或者改变了每页显示数据数量，会保存在cookie中，下回访问时会显示上一次关闭页面时的内容。
            "processing": true,
            "serverSide": true,
            "bLengthChange": false, "bSort": false, //关闭排序功能
            //"pagingType": "bootstrap_full_number",
            'language': {
                'emptyTable': '没有数据',
                'loadingRecords': '加载中...',
                'processing': '查询中...',
                'search': '全局搜索:',
                'lengthMenu': '每页 _MENU_ 件',
                'zeroRecords': '没有您要搜索的内容',
                'paginate': {
                    'first': '第一页',
                    'last': '最后一页',
                    'next': '下一页',
                    'previous': '上一页'
                },
                'info': '第 _PAGE_ 页 / 总 _PAGES_ 页',
                'infoEmpty': '没有数据',
                'infoFiltered': '(过滤总件数 _MAX_ 条)'
            },
            //dataTable 加载加载完成回调函数
            "fnDrawCallback": function (sName, oData, sExpires, sPath) {
                checkJurisdiction(); //调用权限
                form.render();
            },
            "ajax": {
                url: AM.ip + "/categoryChild/getList",
                "dataSrc": function (json) {
                    if (json.code == 200) {
                        return json.data;
                    }
                    return [];
                },
                "data": function (data) {
                    //高级查询参数
                    // data.departmentName = $("#departmentName").val();
                    // data.companyId = $("#companyId").val();
                }
            },
            "columns":[
                {"data": "categoryName"},
                {"data": "childName"},
            ],
            "columnDefs":[
                {
                    "render": function (data, type, row) {
                        var btn = "";
                        if (row.isValid == 1) {
                            btn += "<button onclick='updateStatus(" + row.id + ",0)' class='layui-btn layui-btn-small layui-btn-normal hide checkBtn_11'><i class='fa fa-list fa-edit'></i>&nbsp;删除</button>";
                        }
                        btn += "<button onclick='updateData(" + row.id + ",\""+row.childName+"\")' class='layui-btn layui-btn-small hide checkBtn_12'><i class='fa fa-list fa-edit'></i>&nbsp;修改</button>";
                        return  btn;
                    },
                    "targets": 2
                },
            ]
        });

        $("#search").click(function () {
            dataTable.ajax.reload();
            return false;
        });

    });

    var categoryList = null;

    layui.use(['form', 'layedit', 'laydate'], function () {
        form = layui.form,
            layer = layui.layer;
        // buildAllCompanySelect(0,"companyId");
    });


    //提供给子页面
    var closeNodeIframe = function () {
        dataTable.ajax.reload();
        var index = layer.load(1, {shade: [0.5, '#eee']});
        setTimeout(function () {
            layer.close(index);
        }, 600);
    }





    $(".addBtn").click(function () {

        var html_category = buildCategoryHtml(0);

        var html = '<form class="layui-form" action=""><div class="layui-form-item">' +
            '            <label class="layui-form-label">选择一类<span class="font-red">*</span></label>' +
            '            <div class="layui-input-block">' +
            '                <select name="categoryId" id="categoryId1"  lay-verify="required" lay-search>' + html_category +
            '                </select>' +
            '            </div>' +
            '        </div>' +
            '        <div class="layui-form-item ">' +
            '            <label class="layui-form-label"><span>二类名称</span><span class="font-red">*</span></label>' +
            '            <div class="layui-input-inline">' +
            '                <input type="text" name="childName" lay-verify="required" placeholder="请输入二类名称" autocomplete="off" class="layui-input" maxlength="100">' +
            '            </div>' +
            '        </div></form>';

        layer.open({
            content: html,
            area: ['500px','300px'],
            yes: function(index, layero){
                var childName = $("input[name='childName']").val();
                var categoryId = $("#categoryId1").val();

                if(null == categoryId || 0 == categoryId){
                    layer.msg("没有选择一类");
                    return;
                }

                if(null == childName || childName.length == 0){
                    layer.msg("二类名称为空");
                    return;
                }

                var arr = {
                    childName : childName,
                    categoryId : categoryId
                }

                AM.ajaxRequestData("post", false, AM.ip + "/categoryChild/save", arr, function (result) {
                    if (result.flag == 0 && result.code == 200) {
                        layer.msg('操作成功.', {icon: 1});
                        layer.close(index);
                        dataTable.ajax.reload();
                        setTimeout(function () {
                            layer.closeAll();
                        }, 600);
                    }
                });
            }
        });
        form.render('select')
    })


    //查看/修改数据
    function updateData(id,categoryName) {
        var html = '<form class="layui-form" action="">' +
            '        <div class="layui-form-item ">' +
            '            <label class="layui-form-label"><span>二类名称</span><span class="font-red">*</span></label>' +
            '            <div class="layui-input-inline">' +
            '                <input type="text" name="categoryName" value="'+categoryName+'" lay-verify="required" placeholder="请输入二类名称" autocomplete="off" class="layui-input" maxlength="100">' +
            '            </div>' +
            '        </div></form>';

        layer.open({
            content: html,
            area: ['500px','300px'],
            yes: function(index, layero){

                var categoryName = $("input[name='categoryName']").val();

                if(null == categoryName || categoryName.length == 0){
                    layer.msg("一类名称为空");
                    return;
                }
                var arr = {
                    id: id,
                    childName : categoryName
                }

                AM.ajaxRequestData("post", false, AM.ip + "/categoryChild/update", arr, function (result) {
                    if (result.flag == 0 && result.code == 200) {
                        layer.msg('操作成功.', {icon: 1});
                        layer.close(index);
                        dataTable.ajax.reload();
                        setTimeout(function () {
                            layer.closeAll();
                        }, 600);
                    }
                });
            }
        });
        form.render('select')
    }


    //修改
    function updateStatus(id, status) {
        var statusMsg = "是否删除?";
        var arr = {
            id : id,
            isValid : status
        };
        layer.confirm(statusMsg, function(index){
            AM.ajaxRequestData("post", false, AM.ip + "/categoryChild/update", arr, function (result) {
                if (result.flag == 0 && result.code == 200) {
                    dataTable.ajax.reload();
                    layer.msg('操作成功.', {icon: 1});
                    var index = layer.load(1, {shade: [0.5, '#eee']});
                    setTimeout(function () {
                        layer.close(index);
                    }, 600);
                }
            });
            layer.close(index);
        });

    }



    function buildCategoryHtml(selectId){
        if(null == categoryList){
            AM.ajaxRequestData("post", false, AM.ip + "/qualityCategory/getAll", {} , function(result){
                if(result.flag == 0 && result.code == 200){
                    categoryList = result.data;
                }
            });
        }
        var html = "<option value=\"\">请选择</option>";
        for(var i = 0; i < categoryList.length; i++){

            if(selectId == categoryList[i].id){
                html += "<option value=\"" + categoryList[i].id + "\" selected>" + categoryList[i].categoryName + "</option>";
            }
            else{
                html += "<option value=\"" + categoryList[i].id + "\">" + categoryList[i].categoryName + "</option>";
            }
        }
        if (categoryList.length == 0) {
            html += "<option value=\"0\" disabled>暂无</option>";
        }
        return html;
    }







</script>
</body>
</head>
</html>
